package com.fxw.mylibrary;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;

import com.fxw.mylibrary.util.PermissionsUtil;

/**
 * @Title:   授权处理
 * @Description:
 * @Author:  xianwen.fu
 * @Date:    2016/11/30 12:34
 * @Version: 1.1.0
 */

public class PermissionActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            this.handleIntent(this.getIntent());
        }

    }

    protected void onNewIntent(Intent intent) {
        this.handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        String[] permissions = intent.getStringArrayExtra("permissions");
        int requestCode = intent.getIntExtra("requestCode", 42);
        ActivityCompat.requestPermissions(this, permissions, requestCode);
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        PermissionsUtil.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
        this.finish();
    }
}
